package action.zpcj;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.Const;
import com.PublicSystem;
import com.syslog.SysLogServices;
import com.util.Component;
import com.util.GetFileMd5;
import com.util.Util;
import services.zpcj.ZpcjServices;
/**
* 照片采集
* @author bjh
* @version Fri May 06 17:59:17 CST 2016 gjx
* @since 1.0
*/
@Controller
@RequestMapping("zpcj.do")
public class ZpcjAction {
	ZpcjServices ss = new ZpcjServices();
	SysLogServices log = SysLogServices.getInstance();
	PublicSystem sys = PublicSystem.getInstance();
	/**
	 * 查询信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunitdata")
	public void getunitdata(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = Component.getQuest("id", request);
		String aae011 = Component.getSession("userid", request);
		String res = ss.check(id);
		HashMap obj = new HashMap();
		if(res.equals("0")) {
			HashMap user = ss.getUnitData(id);			
			obj.put("domainid", "SBK");  //分系统号，见sys_log_bm
			obj.put("log_type", "T05");   //日志类别，见sys_log_bm
			obj.put("userid", aae011); //操作人
			obj.put("rwh", Component.getSession("rwid", request));	//任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义
			obj.put("content", "此人照片采集已采集，社会保障卡号：" + id);
			obj.put("czqd", "Q1");	      //操作渠道，见sys_log_bm
			obj.put("czrlb", "L01");      //操作人类别，见sys_log_bm
			obj.put("datain", id); //输入参数
			obj.put("dataout", "success");//输出参数
			log.sysLogMap(obj, request);
			Component.print(Util.Encode(user), response);
		} else {
			obj.put("domainid", "SBK");  //分系统号，见sys_log_bm
			obj.put("log_type", "T05");   //日志类别，见sys_log_bm
			obj.put("userid", aae011); //操作人
			obj.put("rwh", Component.getSession("rwid", request));	//任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义
			obj.put("content", "此人照片可以采集，社会保障卡号：" + id);
			obj.put("czqd", "Q1");	      //操作渠道，见sys_log_bm
			obj.put("czrlb", "L01");      //操作人类别，见sys_log_bm
			obj.put("datain", id); //输入参数
			obj.put("dataout", "success");//输出参数
			log.sysLogMap(obj, request);
			Component.print(Util.Encode(res), response);
		}
	}	
	
	
	/**
	 * 根据身份证号查询社会保障卡号
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getCard")
	public void getCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = Component.getQuest("sfz", request);
	
		String res = ss.getSfz(id);
		
		Component.print(Util.Encode(res), response);
		
	}	
	
	
	
	
	/**
	 * 民族下拉框选项
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getaac005")
	public void getSelectaac005(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List data = ss.getSelect(Component.getQuest("lx", request));
		Component.print(Util.Encode(data), response);
	}	
	/**
	 * 查询单位信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunit")
	public void getunit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = Component.getQuest("id", request);
		String res = ss.checkuint(id);		
		Component.print(Util.Encode(res), response);
	}	
	/**
	 * 查询信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getEventid")
	public void getEventid(HttpServletRequest request, HttpServletResponse response) throws Exception {		
		HashMap obj = new HashMap();
		String res = ss.getEventid(obj);		
	    Component.print(Util.Encode(res), response);	
	}
	/**
	 * 校验批量采集
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=chkzpplcj")
	public void chkzpplcj(HttpServletRequest request, HttpServletResponse response) throws Exception {		
		String id = Component.getQuest("eventid", request);
		String aae011 = Component.getSession("userid", request);		
		HashMap map = new HashMap();
		map.put("eventid", id);
		HashMap res = ss.chkzpplcj(map);
		HashMap obj = new HashMap();
		obj.put("domainid", "SBK");  //分系统号，见sys_log_bm
		obj.put("log_type", "T05");   //日志类别，见sys_log_bm
		obj.put("userid", aae011); //操作人
		obj.put("rwh", Component.getSession("rwid", request));	//任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义
		obj.put("content", "照片批量采集校验，业务号："+id);
		obj.put("czqd", "Q1");	      //操作渠道，见sys_log_bm
		obj.put("czrlb", "L01");      //操作人类别，见sys_log_bm
		obj.put("datain", id); //输入参数
		obj.put("dataout", "success");//输出参数
		log.sysLogMap(obj, request);
	    Component.print(Util.Encode(res), response);	
	}	
	/**
	 * 查询正确信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getsuccess")
	public void getsuccess(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String key = Component.getQuest("key", request);			
		HashMap parms = sys.getGridSort(request);		
		String json = Util.Encode(ss.getsuccess(new Object[] {key}, parms));		
		Component.print(Util.Encode(json), response);
	} 
	/**
	 * 查询错误信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getfail")
	public void getfail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String key = Component.getQuest("key", request);			
		HashMap parms = sys.getGridSort(request);		
		String json = Util.Encode(ss.getfail(new Object[] {key}, parms));		
		Component.print(Util.Encode(json), response);
	} 
	/**
	 * 查询错误信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=savepic")
	public void savepic(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String key = Component.getQuest("eventid", request);	
		String aab001 = Component.getQuest("aab001", request);
		String aae011 = Component.getSession("userid", request);
		String path = Const.server_path + "/plupload/"+key;
		List<File> myfile = new ArrayList<File>();
	    List<String> list = ss.getAcc001s(key);		     
	    File file = new File(path);
	    int num = 0;
	    long maxsize = 200*1024;
		long minsize = 6*1024;				  
		String str = "";
	    List<File> mylist = listDirectory(file, myfile);	    
        for (int i = 0; i < mylist.size(); i++) {
        	long size = mylist.get(i).length();	
        	String aa[] = mylist.get(i).toString().split("/");
        	String name = aa[aa.length-1];
        	if(size < minsize || size > maxsize) {   			    		
    			str = "fail" + name.toString();    			
    			Component.print(str, response);
    			return;
    		}
		}
        for (int i = 0; i < mylist.size(); i++) {
			String aa[] = mylist.get(i).toString().split("/");
			String name = aa[aa.length-1].substring(0, 10);
		
			for(String aac001 : list) {
				if(name.equals(aac001)) {
					String md5 = GetFileMd5.getFileMD5String(new File(mylist.get(i).toString()));
					HashMap row1 = new HashMap();
	                row1.put("aac001", aac001);
	                HashMap row2 = new HashMap();                   
	                row2.put("aac001", aac001);
	                row2.put("aab001", aab001); 
	                row2.put("aaz755", md5); 
	                row2.put("aaz754", (int)mylist.get(i).length());
	                row2.put("aae011", aae011);                
	                num = ss.insertUnit(row1, aac001, row2, mylist.get(i).toString()) + num;
				}
			}
		}      
        HashMap obj = new HashMap();
		obj.put("domainid", "SBK");  //分系统号，见sys_log_bm
		obj.put("log_type", "T02");   //日志类别，见sys_log_bm
		obj.put("userid", aae011); //操作人
		obj.put("rwh", Component.getSession("rwid", request));	//任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义
		obj.put("content", "照片批量采集登记，业务号：" + key);
		obj.put("czqd", "Q1");	      //操作渠道，见sys_log_bm
		obj.put("czrlb", "L01");      //操作人类别，见sys_log_bm
		obj.put("datain", key); //输入参数
		obj.put("dataout", "success");//输出参数
		log.sysLogMap(obj, request);
		Component.print(Util.Encode(num + ""), response);
	} 
    /**
     * 遍历目录及其子目录下的所有文件并保存
     * @param path 目录全路径
     * @param myfile 列表：保存文件对象
     * @return 
     */
    public static List<File> listDirectory(File path, List<File> myfile) {
        if (!path.exists()) {
            System.out.println("文件名称不存在!");
        }
        else {
            if (path.isFile()) {
                myfile.add(path);
            } else {
                File[] files = path.listFiles();
                for (int i = 0; i < files.length; i++ ) {
                    listDirectory(files[i], myfile);
                }
            }
        }
        return myfile;
    }
    /**
	* 卡厂回盘数据数据导出
	* @author duy
	* @param request
	* @param response
	* @throws Exception
	*/
	@RequestMapping(params = "method=exkcdate")
	public void exkcdate(HttpServletRequest request, HttpServletResponse response) throws Exception {	
		String eventid = Component.getQuest("eventid", request);
	    File f = ss.exkcdate(eventid);
	    String userid = Component.getSession("userid", request);
	    HashMap obj = new HashMap();
		obj.put("domainid", "SBK");  //分系统号，见sys_log_bm
		obj.put("log_type", "T06");   //日志类别，见sys_log_bm
		obj.put("userid", userid); //操作人
		obj.put("rwh", Component.getSession("rwid", request));	      //任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义
		obj.put("content", "错误数据文件导出业务号：" + eventid);  //日志内容，自己组合拼装
		obj.put("czqd", "Q1");	      //操作渠道，见sys_log_bm
		obj.put("czrlb", "L01");      //操作人类别，见sys_log_bm
		obj.put("datain", eventid); //输入参数
		obj.put("dataout", "success");//输出参数
		log.sysLogMap(obj, request);
		Util.downLoadData(response, f, "export.xls");
	}
	/**
	 * 公安接口获取照片
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getgaservice")
	public void getgaservice(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = Component.getQuest("id", request);
	}	
}
